package com.qf.spring01.utils;

import org.springframework.beans.factory.FactoryBean;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class MyProxyFactorybean implements FactoryBean {
    private Object object;

    public void setObject(Object object) {
        this.object = object;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }

    @Override
    public Object getObject() throws Exception {
        Object o = Proxy.newProxyInstance(this.object.getClass().getClassLoader(), this.object.getClass().getInterfaces(), new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                long start = System.currentTimeMillis();

                Object invoke = method.invoke(object, args);

                long end = System.currentTimeMillis();
                System.out.println("消耗时间：" + (end - start));

                return invoke;
            }
        });
        return o;
    }

    @Override
    public Class<?> getObjectType() {
        return this.object.getClass();
    }
}
